package com.grep.base;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

import com.grep.base.db.DBUtils;
import com.grep.base.servlet.BaseSevlet;

public class InitDBBase {
	private static Logger logger = LoggerFactory.getLogger(InitDBBase.class);   
	private String logconfig = "config/logback.xml";
	private String dbconfig = "config/dbconfig.properties";
	public InitDBBase(){
		//初始化slf4j日志
		LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); 
		try { 
			JoranConfigurator configurator = new JoranConfigurator(); 
			configurator.setContext(context); 
			// Call context.reset() to clear any previous configuration, e.g. default  
			// configuration. For multi-step configuration, omit calling context.reset(). 
		 	context.reset();  
		 	configurator.doConfigure(InitDBBase.class.getClassLoader().getResourceAsStream(logconfig)); 
		} catch (JoranException je) { 
			je.printStackTrace() ;
		} 
		StatusPrinter.printInCaseOfErrorsOrWarnings(context); 
		
		//初始化c3p0数据库连接池
		logger.info("[read config] start read dbconfig.") ;
		InputStream in = BaseSevlet.class.getClassLoader().getResourceAsStream(dbconfig);
		Properties p= new Properties();
		try {
			p.load(in) ;
		} catch (IOException e) {
			e.printStackTrace();
		}
		logger.info("[read config] read dbconfig end.") ;
		DBUtils.setPro(p) ;
		DBUtils.init() ;
	}
}
